package com.dhcc.sdc.cdss.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.entity.SdcQcCdssLinkEntity;
import com.dhcc.sdc.base.service.ICdssStandardMatchService;
import com.dhcc.sdc.base.service.ISdcQcCdssLinkService;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import com.dhcc.sdc.busi.service.ISdcQcService;
import com.dhcc.sdc.cdss.dao.SdcCdssExamMapper;
import com.dhcc.sdc.cdss.dao.SdcCdssLabMapper;
import com.dhcc.sdc.cdss.dto.CdssLabAndExamDto;
import com.dhcc.sdc.cdss.entity.SdcCdssExamEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssLabEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssLabService;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 表数据服务层接口
 *
 * @ClassName: SdcCdssLabServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 15:05:47
 */
@Service("sdcCdssLabService")
public class SdcCdssLabServiceImpl extends BaseServiceImpl<SdcCdssLabMapper, SdcCdssLabEntity> implements ISdcCdssLabService {

    @Resource
    private SdcCdssExamMapper cdssExamMapper;
    @Autowired
    private ISdcQcService sdcQcService;
    @Autowired
    private ISdsStandSourceService standSourceService;
    @Autowired
    private ISdsStandSourceFieldService sdsStandSourceFieldService;
    @Autowired
    private ICdssStandardMatchService cdssStandardMatchService;
    @Autowired
    private ISdcQcCdssLinkService sdcQcCdssLinkService;

    @Override
    public List<SdcCdssLabEntity> page(Page<SdcCdssLabEntity> page, Map<String, Object> map) {
        List<SdcCdssLabEntity> page1 = this.baseMapper.page(page, map);
        for (SdcCdssLabEntity cdssLabEntity : page1){
            SdcQcCdssLinkEntity sdcQcCdssLinkEntity = sdcQcCdssLinkService.selectByNodeId(cdssLabEntity.getNodeId());
            if (!StringUtils.isEmpty(sdcQcCdssLinkEntity)){
                SdcQcEntity sdcQcEntity = sdcQcService.selectById(sdcQcCdssLinkEntity.getQcItemId());
                if (!StringUtils.isEmpty(sdcQcEntity)){
                    cdssLabEntity.setQcDesc(sdcQcEntity.getQcDesc());
                }
            }
            if (!StringUtils.isEmpty(cdssLabEntity.getDataSource()) && !StringUtils.isEmpty(cdssLabEntity.getDataSourceItem())){
                /*SdsStandSourceEntity sourceEntity = standSourceService.selectById(cdssLabEntity.getDataSource());
                SdsStandSourceFieldEntity sourceFieldEntity = sdsStandSourceFieldService.selectById(cdssLabEntity.getDataSourceItem());
                if (!StringUtils.isEmpty(sourceEntity) && !StringUtils.isEmpty(sourceFieldEntity)){
                    cdssLabEntity.setReferenceTime(sourceEntity.getSourceDesc() + "-" + sourceFieldEntity.getFieldDesc());
                }*/
                String referenceTime = cdssStandardMatchService.handleReferenceTime(SdcCdssLabEntity.class, cdssLabEntity);
                cdssLabEntity.setReferenceTime(referenceTime);
            }
        }
        return page1;
    }

    @Override
    public List<SdcCdssLabEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public String handleJsonArrayData(JSONArray jsonArray, Long qcEntityId, List<String> cdssLabNodeIds, List<String> cdssExamNodeIds) {
        String res;
        int eaxmCount = 0;
        int labCount = 0;
        List<SdcCdssLabEntity> cdssLabEntities = new ArrayList<>();
        List<SdcCdssExamEntity> cdssExamEntities = new ArrayList<>();
        List<CdssLabAndExamDto> cdssLabAndExamEntities = JSONObject.parseArray(jsonArray.toJSONString(), CdssLabAndExamDto.class);
        for (CdssLabAndExamDto cdssLabAndExamDto : cdssLabAndExamEntities){
            String itemType = cdssLabAndExamDto.getItemType();
            if (Objects.equals("126", itemType)){
                //检验
                SdcCdssLabEntity  cdssLabEntity = new SdcCdssLabEntity();
                BeanUtils.copyProperties(cdssLabAndExamDto, cdssLabEntity);
                cdssLabEntities.add(cdssLabEntity);
            }else if (Objects.equals("122", itemType)){
                //检查
                SdcCdssExamEntity  cdssExamEntity = new SdcCdssExamEntity();
                BeanUtils.copyProperties(cdssLabAndExamDto, cdssExamEntity);
                cdssExamEntities.add(cdssExamEntity);
            }
        }

        for (SdcCdssLabEntity cdssLabEntity : cdssLabEntities){
            SdcCdssLabEntity cdssLabEntityNew = new SdcCdssLabEntity();
            cdssLabEntityNew.setQcentityId(qcEntityId);
            cdssLabEntityNew.setItemId(cdssLabEntity.getItemId());
            cdssLabEntityNew.setNodeId(cdssLabEntity.getNodeId());
            cdssLabNodeIds.add(cdssLabEntity.getNodeId());
            SdcCdssLabEntity cdssLabEntity1 = this.baseMapper.selectOne(cdssLabEntityNew);
            if (StringUtils.isEmpty(cdssLabEntity1)){
                //insert
                cdssLabEntity.setQcentityId(qcEntityId);
                cdssLabEntity.setIsCdss(1);
                boolean insert = cdssLabEntity.insert();
                if (insert){
                    labCount++;
                }
            }else {
                //update
                cdssLabEntity1.setQcentityId(qcEntityId);
                cdssLabEntity1.setItemDesc(cdssLabEntity.getItemDesc());
                cdssLabEntity1.setItemName(cdssLabEntity.getItemName());
                cdssLabEntity1.setSourceFlag(cdssLabEntity.getSourceFlag());
                cdssLabEntity1.setNoCondition(cdssLabEntity.getNoCondition());
                cdssLabEntity1.setItemType(cdssLabEntity.getItemType());
                cdssLabEntity1.setCureId(cdssLabEntity.getCureId());
                boolean b = cdssLabEntity1.updateAllColumnById();
                if (b){
                    labCount++;
                }
            }
        }

        for (SdcCdssExamEntity cdssExamEntity : cdssExamEntities){
            SdcCdssExamEntity cdssExamEntityNew = new SdcCdssExamEntity();
            cdssExamEntityNew.setQcentityId(qcEntityId);
            cdssExamEntityNew.setItemId(cdssExamEntity.getItemId());
            cdssExamEntityNew.setNodeId(cdssExamEntity.getNodeId());
            cdssExamNodeIds.add(cdssExamEntity.getNodeId());
            SdcCdssExamEntity cdssExamEntity1 = cdssExamMapper.selectOne(cdssExamEntityNew);
            if (StringUtils.isEmpty(cdssExamEntity1)){
                //insert
                cdssExamEntity.setQcentityId(qcEntityId);
                cdssExamEntity.setIsCdss(1);
                boolean insert = cdssExamEntity.insert();
                if (insert){
                    eaxmCount++;
                }
            }else {
                //update
                cdssExamEntity1.setQcentityId(qcEntityId);
                cdssExamEntity1.setItemDesc(cdssExamEntity.getItemDesc());
                cdssExamEntity1.setItemName(cdssExamEntity.getItemName());
                cdssExamEntity1.setSourceFlag(cdssExamEntity.getSourceFlag());
                cdssExamEntity1.setNoCondition(cdssExamEntity.getNoCondition());
                cdssExamEntity1.setItemType(cdssExamEntity.getItemType());
                cdssExamEntity1.setCureId(cdssExamEntity.getCureId());
                boolean b = cdssExamEntity1.updateAllColumnById();
                if (b){
                    eaxmCount++;
                }
            }
        }

        res = "新增/更新检查" + eaxmCount + "条;" + "新增/更新检验" + labCount + "条";
        return res;
    }

    @Override
    public List<Map<String, Object>> sdcItemList(Map<String, Object> map) {
        return this.baseMapper.sdcItemList(map);
    }

    @Override
    public List<String> selectNodeIds(Long entityId) {
        return this.baseMapper.selectNodeIds(entityId);
    }

    @Override
    public void deleteByNodeIds(List<String> nodeIds) {
        this.baseMapper.deleteByNodeIds(nodeIds);
    }
}
